<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/29
 * Time: 15:08
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class System extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['n_title'] = ['like',"%" . $keyword . "%"];
            }

            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['n_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['n_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['n_time'] = ['< time', $end];
            }
            $map['n_classify'] = 3;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($map)
                ->page($NowPage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
               $n_user_id = trim($vo['n_user_id'],',');
               $user_username= Db::table('up_u_user')->where('user_id','IN',$n_user_id)->column('user_username');
                $vo['user_username'] = implode(',',$user_username);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function add(){
        if(Request::instance()->isPost()){
            $user_id = input('param.user_id');
            $content = input('param.content');
            $m_title = input('param.m_title');
            $n_type = input('param.n_type');
            $data['n_title'] =$m_title;
            $data['n_content'] =$content;
            $data['n_type'] =2;
            $data['n_classify'] =3;
            $data['n_type'] =$n_type;
            if($n_type==2){
                $data['n_user_id'] =','.$user_id.',';
            }
            $data['n_read'] =',0,';
            $data['n_delete'] =',0,';
            $data['n_time'] =date('Y-m-d H:i:s',time());
            $res = Db::table('up_notice')->insertGetId($data);

            if($n_type==2){//代表是个人
                $result = explode(',',$user_id);
                $list = [];
                foreach($result as $k => $val){
                    $v = (string)$val;
                    array_push($list, $v);
                }
                $code=1;
                $str = array('msg'=>$m_title,'type'=>1,'n_id'=>$res);
                $str1 = $m_title;
                try{
                    $this->sendJpush(1, $list, $str1, $str,$code);
                }catch(\Exception $e){

                }
            }elseif ($n_type==1){//代表是全部
                $list = [];
                $code=1;
                $str = array('msg'=>$m_title,'type'=>1,'n_id'=>$res);
                $str1 = $m_title;
                try{
                    $this->sendJpush(2, $list, $str1, $str,$code);
                }catch(\Exception $e){

                }
            }

            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $info = Db::table('up_u_user')->field('user_id,user_username')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function detail(){
      $n_id = input('param.n_id');
        $n_content = Db::table('up_notice')->where('n_id',$n_id)->value('n_content');
        $this->assign('n_content',$n_content);
      return  $this->fetch();
    }
}